package com.longma.server.scheduled;

import cn.hutool.core.util.RandomUtil;
import com.longma.server.dao.YuncangDAO;
import com.longma.server.entity.Yuncang;
import com.longma.server.pojo.dto.RealtimeDataAddDTO;
import com.longma.server.service.IMonthDataService;
import com.longma.server.service.IRealtimeDataService;
import com.longma.server.util.MathUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;


/**
 *  @Author: zy
 *  @Date: 2021/5/17 23:30
 *  @Description: 定时任务
 */
@Component
public class ScheduledTask {

    @Autowired
    private IRealtimeDataService realtimeDataService;

    @Autowired
    private IMonthDataService monthDataService;

    @Autowired
    private YuncangDAO yuncangDAO;

    /*
     * 定时任务  模拟硬件
     */
    @Scheduled(fixedDelay = 30*1000)
    public void receiveDataFromYc()  {
        // 遍历云仓id
        List<Yuncang> yuncangs = yuncangDAO.list();

        for(Yuncang yuncang:yuncangs) {
            Integer yuncangId = yuncang.getId();
            RealtimeDataAddDTO data = new RealtimeDataAddDTO();

            data.setCollectorSn(yuncang.getCollectorSn());
            double lat;
            double lng;
            if(yuncangId < 4){
                lat = 31.2 +yuncangId*0.1;
                lng = 120 +yuncangId*0.1;
            }else {
                lat = 30 + RandomUtil.randomDouble(0.05) + yuncangId * 0.2;
                lng = 118 + RandomUtil.randomDouble(0.1) + yuncangId * 0.4;
            }
            data.setLatitude(MathUtil.round(lat));
            data.setLongitude(MathUtil.round(lng));

            // 电仓系统电压 -100 - 10050 正常   电压*电流600kW
            Double voltage = MathUtil.round(9950 + RandomUtil.randomDouble(110));
            data.setVoltage(voltage);

            // 电仓系统电流 -100-70 正常
            Double current = MathUtil.round(55 + RandomUtil.randomDouble(10));
            data.setCurrent(current);

            // 发电功率
            data.setPvPower(MathUtil.round(voltage*current/1000.0));

            // 电仓室内温度
            data.setTemperature(MathUtil.round(2 * Math.random() + 25));
            // 电仓室内湿度
            data.setHumidity(MathUtil.round(0.2 * Math.random() + 0.3));

            // 模拟日发电量 日用电量
            int hour = LocalDateTime.now().getHour();
            int minute = LocalDateTime.now().getMinute();

            // 1小时发电量 600kWh   功率为600kW  1分钟发电10kWh
            data.setDailyGeneration(MathUtil.round((hour * 60 + minute) * 10));
            // 1小时用电量 60kWh   功率为6kW  1分钟用电1kWh
            data.setDailyConsumption(MathUtil.round(hour * 60 + minute));

            // 电池情况
            data.setBatteryTemp(MathUtil.round(2 * Math.random() + 25));
            Double batteryVoltage = MathUtil.round(350 + RandomUtil.randomDouble(20));
            Double batteryDischargeCurrent = MathUtil.round(14 + RandomUtil.randomDouble(4));
            // 充电
            data.setBatteryChargeCurrent(0.0);
            data.setBatteryChargePower(0.0);
            // 放电
            data.setBatteryDischargeCurrent(batteryDischargeCurrent);
            data.setBatteryDischargePower(MathUtil.round(batteryVoltage*batteryDischargeCurrent/1000.0));
            data.setBatteryVoltage(batteryVoltage);
            data.setUsedPower(MathUtil.round(batteryVoltage*batteryDischargeCurrent/1000.0));

            // 模拟硬件
            realtimeDataService.receiveDataFromYc(data);
        }
    }

    /*
     * 定时更新每个云仓的月发发电量、月电量数据
     */
    @Scheduled(fixedDelay = 5*60*1000)
    public void handleMonthData() {
        monthDataService.updateMonthData();
    }
}
